<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Breakfast
 *
 * @author Administrator
 */

namespace app\common\model;

use think\Model;
use think\Db;

class WeixinUser extends Model {

    public function wx_add_user($wx_data) {
        $wx_user=Db::name('weixin_user')->where(array('openid'=>$wx_data['openid']))->find();
        if($wx_user){
            return $wx_user['user_id'];
        }
        DB::startTrans();
        try {
            $wx_data['create_time']=NOW_TIME;
            $wx_data['update_time']=NOW_TIME;
            $user_data['account'] = $wx_data['openid'];
            $user_data['password'] = password_hash_tp($wx_data['openid']);
            $user_data['create_time']=NOW_TIME;
            $user_data['update_time']=NOW_TIME;
            $user_data['realname']=$wx_data['nickname'];

            Db::name('admin_user')->strict(false)->insert($user_data);
            $user_id = DB::name('admin_user')->getLastInsID();
//            $model = model("UserTree");
//            $model->stomp_move($user_id, 0);
            $role_date['user_id'] = $user_id;
            $wx_data['user_id'] = $user_id;
            $wx_data['user_info'] = json_encode($wx_data);
            Db::name('weixin_user')->strict(false)->insert($wx_data);
            DB::commit();
            return $user_id;
        } catch (\Exception $e) {
            // 回滚事务
            DB::rollback();
            
//            exception($e);
            return false;
        }
    }

}
